package com.itheima.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.bean.PageParam;
import com.itheima.service.EmployeeService;
import com.itheima.common.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/employee")
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //员工登陆
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession session){

        //1.调用员工
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername,employee.getUsername());

        String md5Pwd= DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        lqw.eq(Employee::getPassword,md5Pwd);

        //追加状态的响应
        //lqw.eq(Employee::getStatus , 1);

        Employee loginEmployee=employeeService.getOne(lqw);

        //2.响应结果
        if(loginEmployee != null){
            session.setAttribute("employee",loginEmployee);
            //成功信息
            return R.success(loginEmployee);
        }
        //失败信息
        return R.error("用户名或密码错误");
    }

    //退出登陆
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();

        return R.success("退出成功");
    }

    //添加员工
    @PostMapping
    public R add(@RequestBody Employee employee, HttpSession session){
        //1.补充数据
        //补充初始1密码
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

//        //创建时间和更新时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //创建用户和更新用户,谁负责添加记录(谁登陆,谁干这个事情)
//        Employee e = (Employee) session.getAttribute("employee");
//        employee.setCreateUser(e.getId());
//        employee.setUpdateUser(e.getId());

        //2.调用service
        boolean result = employeeService.save(employee);

        //3.响应结果
        if(result){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }

    //员工页/分页查询
    @GetMapping("/page")
    public R page(PageParam param){
        //1.构建分页对象
        IPage<Employee> page = new Page<>(param.getPage(), param.getPageSize());
        //2.构造条件对象
        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

        //3.设置查询和条件
        lqw.like(param.getName() != null , Employee::getName , param.getName());

        IPage<Employee> p = employeeService.page(page, lqw);

        //4.返回结果
        return R.success(p);
    }


    //更新修改员工状态或更新员工
    @PutMapping
    public R update(@RequestBody Employee employee, HttpSession session){
//
//        Employee e = (Employee) session.getAttribute("employee");
//        employee.setUpdateUser(e.getId());
//        employee.setUpdateTime(LocalDateTime.now());


        //1.调用service
        boolean result = employeeService.updateById(employee);

        //2.返回结果
        if(result){
            return R.success("更新成功");
        }
        return  R.error("更新失败");
    }

    //根据id查询员工数据
    @GetMapping("/{id}")
    public R findById(@PathVariable long id){
        //1.调用Service
        Employee employee = employeeService.getById(id);

        //2.返回结果
        if(employee != null){
            return R.success(employee);
        }
        return R.error("查询失败!");
    }
}
